"""Test case for django_ldap.backend"""

backend_init = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> backend_obj = Backend(server='test.com',bind_dn='cn=root,dc=test,dc=com',bind_pwd='testpwd')
>>> backend_obj.get_ldapuri()
'ldap://test.com:389'

# test with the wrong password
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd='somepwd')
>>> connection = backend_obj.get_connection()
Traceback (most recent call last):
   ...
LdapBackendException: Invalid authentication credentials

# test with the wrong server
>>> backend_obj = Backend(server='someserver',bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD)
>>> connection = backend_obj.get_connection()
Traceback (most recent call last):
   ...
LdapBackendException: Cannot connect to LDAP server
"""

backend_search = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD,base='ou=test,%s' % settings.LDAP_BASE)
>>> connection = backend_obj.get_connection()
>>> connection is not None
True
>>> import ldap
>>> result = backend_obj.search('ou=test')
>>> result[0]['dn'][0][:7]
u'ou=test'
"""

backend_insert = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> dn = 'cn=testuser3,ou=test,%s' % settings.LDAP_BASE
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD,base='ou=test,%s' % settings.LDAP_BASE)
>>> attrs = {'objectClass':['inetOrgPerson','organizationalPerson','person','top'],'cn':['testuser3'],'sn':['test'],'givenName':['user'],'uid':None,'mail':[],'postalAddress':[None]}
>>> backend_obj.insert(dn, attrs)
>>> obj = backend_obj.search('cn=testuser3')[0]
>>> obj.pop('dn')[0] == dn
True
>>> obj
{'objectClass': [u'inetOrgPerson', u'organizationalPerson', u'person', u'top'], 'givenName': [u'user'], 'sn': [u'test'], 'cn': [u'testuser3']}
"""

backend_modify = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> dn = 'cn=testuser3,ou=test,%s' % settings.LDAP_BASE
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD,base='ou=test,%s' % settings.LDAP_BASE)
>>> attrs = {'sn':['newsn'],'cn':['testuser4'],'givenName':[None],'mail':['test'],'postalAddress':['address']}
>>> backend_obj.modify(dn,attrs)
>>> new_obj = backend_obj.search('cn=testuser4')[0]
>>> new_obj['sn'][0]
u'newsn'
>>> new_obj['givenName']
Traceback (most recent call last):
   ...
KeyError: 'givenName'
>>> attrs = {'sn':['newsn'],'cn':['testuser4'],'mail':[],'postalAddress':None}
>>> backend_obj.modify(new_obj['dn'][0],attrs)
>>> new_obj = backend_obj.search('cn=testuser4')[0]
>>> new_obj['mail']
Traceback (most recent call last):
   ...
KeyError: 'mail'
>>> new_obj['postalAddress']
Traceback (most recent call last):
   ...
KeyError: 'postalAddress'
"""

backend_delete = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD,base='ou=test,%s' % settings.LDAP_BASE)
>>> new_obj = backend_obj.search('cn=testuser4')[0]
>>> backend_obj.delete(new_obj['dn'][0])
>>> attrs = {'cn':['testuser2']}
>>> new_obj = backend_obj.search('cn=testuser1', 'ou=referral,ou=test,%s' % settings.LDAP_BASE)
>>> new_obj
[]
"""

backend_get_all_attrs = """
>>> from django_ldap.backend import Backend
>>> from django.conf import settings
>>> backend_obj = Backend(server=settings.LDAP_SERVER,bind_dn=settings.LDAP_BIND_DN,bind_pwd=settings.LDAP_BIND_PWD,base='ou=test,%s' % settings.LDAP_BASE)
>>> backend_obj.get_all_attributes('inetOrgPerson')
set(['uid'...
"""

ldap_database_operations = """
>>> from django_ldap.backend import DatabaseOperations
>>> db_operations = DatabaseOperations()
>>> db_operations.quote_name('test')
'test'
"""

__test__ = {
    'backend_init': backend_init,
    'backend_search': backend_search,
    'backend_1_insert': backend_insert,
    'backend_modify': backend_modify,
    'backend_z_delete': backend_delete,
    'backend_get_all_attrs': backend_get_all_attrs,
    'ldap_database_operations': ldap_database_operations,
}